<template>
  <!-- 添加或修改个人发票信息管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <!--<el-col :span="12">
                <el-form-item label="发票所属人" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入发票所属人"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="发票所属人姓名" prop="userName">
                    <el-input v-model="form.userName" placeholder="请输入发票所属人姓名"/>
                </el-form-item>
            </el-col>-->
            <!--<el-col :span="12">
                <el-form-item label="报销日期" prop="imburseDate">
                    <el-date-picker clearable
                                    v-model="form.imburseDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择报销日期">
                    </el-date-picker>
                </el-form-item>
            </el-col>-->
            <el-col :span="12">
              <el-form-item label="发票号" prop="taxNo">
                <el-input v-model="form.taxNo" placeholder="请输入发票号" @change="checkTaxNoRepeat" />
                <span style="color: red;margin-left: 20px;" v-if="!noRepeat">发票号重复，请核对！</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发票金额" prop="taxMoney">
                <el-input-number :precision="2" v-model="form.taxMoney" placeholder="请输入发票金额" style="width: 100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票日期" prop="taxDate">
                <el-date-picker clearable v-model="form.taxDate" type="date" value-format="yyyy-MM-dd"
                  placeholder="请选择开票日期" style="width: 100%;">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="发票描述" prop="taxInfo">
                <el-input v-model="form.taxInfo" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
            <!--<el-col :span="24">
                <el-form-item label="附件">
                    <file-upload v-model="form.file"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="删除标志" prop="delFlag">
                    <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>
            </el-col>-->
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    addAction,
    getById,
    updateAction,
    getAction
  } from '@/api/manage';
  import moment from 'moment';

  export default {
    name: "GoodsInvoiceSelfForm",
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          taxNo: [{
            required: true,
            message: "发票号不能为空",
            trigger: "blur"
          }],
          taxMoney: [{
            required: true,
            message: "发票金额不能为空",
            trigger: "blur"
          }],
          taxDate: [{
            required: true,
            message: "发票开具日期不能为空",
            trigger: "blur"
          }],
        },
        url: {
          list: '/goodsInvoiceSelf/goodsInvoiceSelf/list',
          getById: '/goodsInvoiceSelf/goodsInvoiceSelf/',
          add: '/goodsInvoiceSelf/goodsInvoiceSelf/add',
          edit: '/goodsInvoiceSelf/goodsInvoiceSelf/edit',
        },
        noRepeat: true, //发票号不重复
      };
    },
    created() {

    },
    methods: {
      checkTaxNoRepeat() {
        let that = this;
        let taxNo = that.form.taxNo;
        getAction(this.url.list, {
          "taxNo": taxNo
        }).then(response => {
          if (response.total > 0) {
            that.noRepeat = false;
          } else {
            that.noRepeat = true;
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: this.$store.getters.userId,
          userName: this.$store.getters.nickName,
          imburseDate: new Date(),
          taxNo: null,
          taxMoney: "",
          taxDate: new Date(),
          taxInfo: null,
          file: null,
          status: "0",
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加个人发票信息管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getById(this.url.getById, id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改个人发票信息管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid && that.noRepeat) {
            if (that.form.id != null) {
              updateAction(that.url.edit, that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.$emit('ok');
              });
            } else {
              addAction(that.url.add, that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.$emit('ok');
              });
            }
          }
        });
      },
    }
  };
</script>
